//
//  HomeYuGaoParticularsFormView.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/1.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "HomeYuGaoParticularsFormView.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"

@interface HomeYuGaoParticularsFormView ()

@property (nonatomic,strong) UIView *shadowView;

@property (nonatomic,strong) NSMutableArray *mutableArray;

@end

@implementation HomeYuGaoParticularsFormView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.userHeadView];
        [self addSubview:self.trailerImage]; // 预告图片
        [self addSubview:self.trailerLab];   // 预告标题
        [self.userHeadView addSubview:self.userPortrait]; // 用户头像
        [self addSubview:self.userName];     // 用户名称
        [self addSubview:self.timeImage];    // 时间图标
        [self addSubview:self.trailerData];  // 日期
        [self addSubview:self.contentLab];   // 简介
        [self addSubview:self.grabBtn];      // 抢位
        [self.trailerImage addSubview:self.startView];
        [self.startView addSubview:self.startImage];
        [self.startView addSubview:self.startLab];
        
    }
    return self;
}

- (void)setModel:(HomeMeetingFromModel_Two *)model
{
    [_userPortrait sd_setImageWithURL:[NSURL URLWithString:model.head_img] forState:0 placeholderImage:[UIImage imageNamed:@"toixiang"]];
    _userName.text = model.nickname;
    self.mutableArray = [NSMutableArray arrayWithArray:model.industryid];
    _trailerLab.text = model.title;
    [_trailerImage sd_setImageWithURL:[NSURL URLWithString:[model.imgsrc lastObject]]];
    _contentLab.text = [NSString stringWithFormat:@"简介：%@",model.info];
    [UILabel changeLineSpaceForLabel:_contentLab WithSpace:3.f];
    _trailerData.text = [self timeWithTimeIntervalString:[model.create_date stringValue]];
    _startLab.text = [self countdowntimeWithTimeIntervalString:[model.start_date stringValue]];

    [self showMeetingTrailerView];
}

// 时间戳转换 --- 发布时间
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSTimeInterval interval    =[timeString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM.dd HH:mm"]; // yyyy-MM-dd HH:mm:ss
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}

// 时间戳转换 --- 倒计时
- (NSString *)countdowntimeWithTimeIntervalString:(NSString *)timeString
{
    NSTimeInterval interval    =[timeString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"mm:ss"]; // yyyy-MM-dd HH:mm:ss
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}

- (void)showMeetingTrailerView
{
    _trailerImage.sd_layout
    .topSpaceToView (self,0)
    .leftSpaceToView(self,0)
    .rightSpaceToView(self,0)
    .heightIs(kHeightScale(103.5));
    
    _trailerLab.sd_layout
    .bottomEqualToView(self.trailerImage)
    .leftEqualToView(self.trailerImage)
    .rightEqualToView(self.trailerImage)
    .heightIs(kHeightScale(20));
    
    _userHeadView.sd_layout
    .topSpaceToView(self.trailerImage,kHeightScale(5))
    .leftSpaceToView(self,kWidthScale(10))
    .widthIs(kWidthScale(25))
    .heightIs(kHeightScale(25));
    
    _userPortrait.sd_layout
    .topSpaceToView(self.userHeadView, kHeightScale(2))
    .leftSpaceToView(self.userHeadView, kWidthScale(2))
    .rightSpaceToView(self.userHeadView,kWidthScale(2))
    .bottomSpaceToView(self.userHeadView,kHeightScale(2));
    
    _userName.sd_layout
    .topEqualToView(self.userHeadView)
    .leftSpaceToView(self.userHeadView,kWidthScale(5))
    .widthIs(kWidthScale(100))
    .heightIs(kHeightScale(10));
    
    _timeImage.sd_layout
    .topSpaceToView(self.userName,kHeightScale(3))
    .leftEqualToView(self.userName)
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(13));
    
    _trailerData.sd_layout
    .centerYEqualToView(self.timeImage)
    .leftSpaceToView(self.timeImage,kWidthScale(5))
    .widthIs(kWidthScale(100))
    .heightIs(kHeightScale(10));
    
    _grabBtn.sd_layout
    .centerYEqualToView (self.userHeadView)
    .rightSpaceToView(self,kWidthScale(10))
    .widthIs(kWidthScale(60))
    .heightIs(kHeightScale(25));
    
    _contentLab.sd_layout
    .topSpaceToView(self.timeImage,kHeightScale(2))
    .leftSpaceToView(self,kWidthScale(10))
    .rightEqualToView(self.grabBtn)
    .bottomSpaceToView(self,kHeightScale(2));
    
    for (int i = 0; i < self.mutableArray.count; i++) {
        _industryLab = [[UILabel alloc] initWithFrame:CGRectMake(kWidthScale(5) + kWidthScale(40) * i, kHeightScale(5), kWidthScale(35), kHeightScale(15))];
        
        _industryLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _industryLab.text = self.mutableArray[i];
        _industryLab.textColor = [UIColor colorWithWhite:1 alpha:1];
        _industryLab.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _industryLab.textAlignment = NSTextAlignmentCenter;
        _industryLab.layer.cornerRadius = kWidthScale(5);
        _industryLab.layer.masksToBounds = YES;
        
        [self.trailerImage addSubview:_industryLab];
    }
    
    _startView.sd_layout
    .topSpaceToView(self.trailerImage,kHeightScale(5))
    .rightSpaceToView(self.trailerImage,kWidthScale(5))
    .widthIs(kWidthScale(47))
    .heightIs(kHeightScale(15));
    
    _startImage.sd_layout
    .centerYEqualToView(self.startView)
    .leftSpaceToView(self.startView,kWidthScale(5))
    .widthIs(kWidthScale(8))
    .heightIs(kHeightScale(10));
    
    _startLab.sd_layout
    .centerYEqualToView(self.startImage)
    .leftSpaceToView(self.startImage,kWidthScale(2))
    .rightSpaceToView(self.startView,kWidthScale(2))
    .heightIs(kHeightScale(10));
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

// 预告图片
- (UIImageView *)trailerImage
{
    if (!_trailerImage)
    {
        _trailerImage = [[UIImageView alloc] init];
        _trailerImage.image = [UIImage imageNamed:@"ditu"];
    }
    return _trailerImage;
}
// 预告标题
- (UILabel *)trailerLab
{
    if (!_trailerLab)
    {
        _trailerLab = [[UILabel alloc] init];
        _trailerLab.textAlignment = NSTextAlignmentCenter;
        _trailerLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        _trailerLab.text = @"为什么要读大学";
        _trailerLab.textColor = [UIColor whiteColor];
        _trailerLab.font = [UIFont systemFontOfSize:kWidthScale(14)];
    }
    return _trailerLab;
}
// 用户头像
- (UIButton *)userPortrait
{
    if (!_userPortrait)
    {
        _userPortrait = [UIButton buttonWithType:UIButtonTypeCustom];
        [_userPortrait setBackgroundImage:[UIImage imageNamed:@"toixiang"] forState:0];
        _userPortrait.layer.cornerRadius = kWidthScale(11);
        _userPortrait.layer.masksToBounds = YES;
    }
    return _userPortrait;
}
// 用户名称
- (UILabel *)userName
{
    if (!_userName)
    {
        _userName = [[UILabel alloc] init];
        _userName.text = @"梦想赢家";
        _userName.font = [UIFont systemFontOfSize:kWidthScale(12)];
        _userName.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _userName;
}
// 时间图标
- (UIImageView *)timeImage
{
    if (!_timeImage)
    {
        _timeImage = [[UIImageView alloc] init];
        _timeImage.image = [UIImage imageNamed:@"time"];
    }
    return _timeImage;
}
// 日期
- (UILabel *)trailerData
{
    if (!_trailerData)
    {
        _trailerData = [[UILabel alloc] init];
        _trailerData.text = @"2107.09.03";
        _trailerData.font = [UIFont systemFontOfSize:kWidthScale(11)];
        _trailerData.textColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _trailerData;
}
// 简介
- (UILabel *)contentLab
{
    if (!_contentLab)
    {
        _contentLab = [[UILabel alloc] init];
        _contentLab.text = @"简介：读大学，哪怕不是一本二本，也可以让你在直面残酷的社会之前有个缓冲，不至于被虐的太惨。";
        _contentLab.numberOfLines = 0;
        _contentLab.font = [UIFont systemFontOfSize:kWidthScale(11)];
        _contentLab.textColor = [UIColor colorWithHexString:@"#666666"];
        [UILabel changeLineSpaceForLabel:_contentLab WithSpace:2.0f];
    }
    return _contentLab;
}
- (UIButton *)grabBtn
{
    if (!_grabBtn)
    {
        _grabBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_grabBtn setBackgroundImage:[UIImage imageNamed:@"huiyiqiangwei"] forState:UIControlStateNormal];
//        [_grabBtn setImage:[UIImage imageNamed:@"huiyiqiangwei"] forState:0];
    }
    return _grabBtn;
}
- (UIImageView *)userHeadView
{
    if (!_userHeadView)
    {
        _userHeadView = [[UIImageView alloc] init];
        _userHeadView.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];;
        _userHeadView.layer.masksToBounds=YES;
        _userHeadView.layer.cornerRadius=12.5;
        _userHeadView.layer.shadowColor = [UIColor blackColor].CGColor;
        _userHeadView.layer.shadowOffset = CGSizeMake(-3, 1);
        _userHeadView.layer.shadowOpacity = 0.4;
        _userHeadView.layer.shadowRadius = 2.0;
        _userHeadView.clipsToBounds = NO;
        
        _userHeadView.image = [UIImage imageNamed:@"touxiang"];;
        _userHeadView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _userHeadView;
}

- (UIView *)startView
{
    if (!_startView)
    {
        _startView = [[UIView alloc] init];
        _startView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        _startView.layer.cornerRadius = kWidthScale(5);
        _startView.layer.masksToBounds = YES;
    }
    return _startView;
}

- (UIImageView *)startImage
{
    if (!_startImage)
    {
        _startImage = [[UIImageView alloc] init];
        _startImage.image = [UIImage imageNamed:@"daojishi"];
        _startImage.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _startImage;
}

- (UILabel *)startLab
{
    if (!_startLab)
    {
        _startLab = [[UILabel alloc] init];
        _startLab.textColor = [UIColor whiteColor];
        _startLab.font = [UIFont systemFontOfSize:kWidthScale(10)];
    }
    return _startLab;
}

@end
